package com.ruoyi.business.customer.service.impl;


import com.ruoyi.business.customer.domain.JbFollowupRecord;
import com.ruoyi.business.customer.domain.dto.JbFollowupEvaluationDto;
import com.ruoyi.business.customer.service.JbFollowupEvaluationService;
import com.ruoyi.business.customer.service.JbFollowupRecordService;
import com.ruoyi.business.sundry.service.JbEvaluationExampleService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.base.JoBaseServiceImpl;
import com.ruoyi.business.customer.mapper.JbFollowupEvaluationMapper;
import com.ruoyi.business.customer.domain.JbFollowupEvaluation;
import org.springframework.transaction.annotation.Transactional;

import java.util.function.Function;

/**
 * 跟进记录评价Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
public class JbFollowupEvaluationServiceImpl
        extends JoBaseServiceImpl<JbFollowupEvaluationMapper, JbFollowupEvaluation>
        implements JbFollowupEvaluationService
{
    @Autowired
    private JbFollowupRecordService followupRecordService;
    @Autowired
    private JbEvaluationExampleService evaluationExampleService;
    @Override
    @Transactional
    public Boolean saveInfo(JbFollowupEvaluationDto jbFollowupEvaluationDto) {
        JbFollowupRecord record = followupRecordService.getById(jbFollowupEvaluationDto.getRecordId());
        JbFollowupEvaluation followupEvaluation=new JbFollowupEvaluationDto();
        BeanUtils.copyProperties(jbFollowupEvaluationDto,followupEvaluation);
        this.save(followupEvaluation);
        if (ObjectUtils.isNotEmpty(jbFollowupEvaluationDto.getNextFollowupRecord())){
            jbFollowupEvaluationDto.getNextFollowupRecord().setCustomerId(record.getCustomerId());
            jbFollowupEvaluationDto.getNextFollowupRecord().setDataType(Constants.PUBLIC_NUMBER_0);
            jbFollowupEvaluationDto.getNextFollowupRecord().setTaskStatus(Constants.JBFOLLOWUPRECORD_TASKSTATUS_1001);
            followupRecordService.save(jbFollowupEvaluationDto.getNextFollowupRecord());
        }
        if (ObjectUtils.isNotEmpty(jbFollowupEvaluationDto.getEvaluationExampleList())){
            jbFollowupEvaluationDto.getEvaluationExampleList().forEach(a ->{
                a.setEvaluationId(followupEvaluation.getId());
                a.setFollowupId(jbFollowupEvaluationDto.getRecordId());
            });
            evaluationExampleService.saveBatch(jbFollowupEvaluationDto.getEvaluationExampleList());
        }
        return true;
    }
}
